package com.yyj.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yyj.dao.TPropertyAdviceMapper;
import com.yyj.exception.BaseException;
import com.yyj.model.TPropertyAdvice;
import com.yyj.model.TPropertyRepairRecord;
import com.yyj.web.protocol.PageRspData;
import com.yyj.web.protocol.req.AdviceAddReq;
import com.yyj.web.protocol.req.AdviceQueryReq;
import com.yyj.web.protocol.req.AdviceReplyReq;
import com.yyj.web.protocol.resp.RepairRecordResp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class AdviceService {

    @Autowired
    TPropertyAdviceMapper adviceMapper;

    @Autowired
    UserService userService;

    @Transactional(rollbackFor = Exception.class)
    public void addAdvice(AdviceAddReq adviceAddReq){
        TPropertyAdvice tPropertyAdvice = new TPropertyAdvice();
        BeanUtils.copyProperties(adviceAddReq , tPropertyAdvice);
        tPropertyAdvice.setCreateTime(new Date());
        tPropertyAdvice.setCreator(userService.findOwnerByMobile(adviceAddReq.getMobile() , adviceAddReq.getRoomNum()).getId());
        int insertRlt = adviceMapper.insert(tPropertyAdvice);
        if(1 == insertRlt){

        }


    }

    public TPropertyAdvice queryAdviceById(Long id){
        return adviceMapper.selectByPrimaryKey(id);
    }

    public PageRspData<TPropertyAdvice> queryAdvice(AdviceQueryReq adviceQueryReq){
        Page page = PageHelper.startPage(adviceQueryReq.getCurrentPage(), adviceQueryReq.getPageSize());
        List<TPropertyAdvice> adviceList = adviceMapper.queryAdviceRecord(adviceQueryReq);
        PageRspData<TPropertyAdvice> pageRspData = new PageRspData<TPropertyAdvice>(
                page.getTotal(), adviceQueryReq.getCurrentPage(), adviceQueryReq.getPageSize(), adviceList);
        return pageRspData;
    }

    @Transactional(rollbackFor = Exception.class)
    public TPropertyAdvice ownerReply(AdviceReplyReq adviceReplyReq){
        TPropertyAdvice tPropertyAdvice = adviceMapper.selectByPrimaryKey(adviceReplyReq.getId());
        if(null == tPropertyAdvice){
            throw new BaseException("投诉建议记录不存在");
        }
        tPropertyAdvice.setOwnerReply(adviceReplyReq.getReplyContent());
        adviceMapper.updateByPrimaryKeySelective(tPropertyAdvice);
        return tPropertyAdvice;
    }

    @Transactional(rollbackFor = Exception.class)
    public TPropertyAdvice managerReply(AdviceReplyReq adviceReplyReq){
        TPropertyAdvice tPropertyAdvice = adviceMapper.selectByPrimaryKey(adviceReplyReq.getId());
        if(null == tPropertyAdvice){
            throw new BaseException("投诉建议记录不存在");
        }
        if(StringUtils.isBlank(tPropertyAdvice.getFollowStatement1()) && StringUtils.isBlank(tPropertyAdvice.getFollowStatement2())){
            tPropertyAdvice.setFollowStatement1(adviceReplyReq.getReplyContent());
        }else{
            tPropertyAdvice.setFollowStatement2(adviceReplyReq.getReplyContent());
        }
        adviceMapper.updateByPrimaryKeySelective(tPropertyAdvice);
        return tPropertyAdvice;
    }

}
